/*
 * Copyright (C) 2012 Futon Redemption
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.widgetpreview;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.os.Environment;
import android.util.Log;
import android.view.View;

public class ViewImageFactory {

	private static final String LOG_TAG = "ViewImageFactory";
	
	private final View mTarget;
	
	public ViewImageFactory(final View target) {
		this.mTarget = target;
	}
	
	public Bitmap getBitmap() {
        mTarget.invalidate();
        final Bitmap bmp = Bitmap.createBitmap(mTarget.getWidth(), mTarget.getHeight(), Config.ARGB_8888);
        final Canvas c = new Canvas(bmp);
        mTarget.draw(c);
        return bmp;
    }
	
	private Resources getResources() {
		return mTarget.getResources();
	}
	
	public File buildFile(String name) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return null;
        }

        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS);
        int orientationCode = getResources().getConfiguration().orientation;
        String orientation;
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            orientation = "landscape";
        } else if (orientationCode == Configuration.ORIENTATION_PORTRAIT) {
            orientation = "portrait";
        } else if (orientationCode == Configuration.ORIENTATION_SQUARE) {
            orientation = "square";
        } else {
            orientation = "undefined";
        }
        return new File(path, name + "_ori_" + orientation + ".png");
    }
	
	public boolean save(String name) {
		final Bitmap bmp = getBitmap();
		
        File pic = buildFile(name);
        if (pic == null) {
            Log.d(LOG_TAG, "External storage not present");
            return false;
        }

        pic.getParentFile().mkdirs();
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(pic);
            if (!bmp.compress(CompressFormat.PNG, 100, fout)) {
                Log.d(LOG_TAG, "Failed to compress image");
                return false;
            }
            return true;
        } catch (IOException e) {
            Log.d(LOG_TAG, "Error writing to disk: " + e);
        } finally {
            try {
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException e) {
                Log.d(LOG_TAG, "Could not close file: " + e);
            }
        }
        return false;
    }
}
